﻿
$(function () {
    RecursiveCall();

    $("#accordion").accordion({
        autoHeight: false,
        navigation: true,
        collapsible: true,
        active: false,
        changestart: function (event, ui) {
            var url = ui.newHeader.find('a').attr('href');
            ui.newContent.load(url);
        }
    });
});

$(document).ready(function () {
    $("#MessageContent").scrollTop(300);
});

function AddMember() {
    var fullName = $('#PeopleList option:selected').text();
    var id = $('#PeopleList option:selected').val();
    if (id != null && id != 0) {
        $('#MembersList').append("<li>" + fullName + "</li>");
        $("#PeopleList option:selected").remove();
        var t = "<input id='MemberId' name='MemberId' type='hidden' value='" + id + "'>";

        $('<input>').attr({
            type: 'hidden',
            id: 'MemberId',
            name: 'MemberId',
            value: id
        }).appendTo('#partyForm');
    }
}

function OpenFavoriteTeamSelection(tournamentId) {
    var url = "/Teams/TeamSelection?tournamentId=" + tournamentId;

    var actions = {
        "Cancel": function () {
            $(this).dialog("close");
        }
    };

    $("#popup-message").html("<b>Loading...</b>").load(url);

    $("#popup-message").dialog({
        title: "Matches",
        modal: true,
        width: 675,
        height: 600,
        buttons: actions
    });
}

function SetFavoriteTeam(teamId) {
    $('#popup-message').dialog("close");
    var partyId = $('#PartyId').val();
    var url = "/Parties/SetFavoriteTeam";
    var formValues = "partyId=" + partyId + "&teamId=" + teamId;

    $.post(url, formValues, function (response) {
        location.reload();
    });
}

function PublishMessage(event, partyId) {
    if(event.keyCode == 13){
        var url = "/Parties/PublishMessage";
        var message = $('#ChatMessage').val();
        var formValues = "partyId=" + partyId + "&message=" + message;
        $.post(url, formValues, function (response) {
            if (response.Status) {
                $('#chatResult').hide();
                ReloadChatWindow();
            }
            else {
                $('#chatResult').html(response.Messages);
                $('#chatResult').show();
            }
            $('#ChatMessage').val("");
        });
    }
}

function RecursiveCall() {
    ReloadChatWindow();
    setTimeout("RecursiveCall()", 10000);
}

function ReloadChatWindow() {
    var partyId = $('#PartyId').val();
    var url = "/Parties/Messages";
    var formValues = "partyId=" + partyId;
    $.get(url, formValues, function (response) {
        $('#MessageContent').html(response);
        $("#MessageContent").scrollTop(3000);
    });
}